﻿<%@ Page Title="系统设置" Language="C#" MasterPageFile="~/master/m.master" %>
<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
<style type="text/css">
.pageControl{
	position: absolute;
	bottom: 0;
	left: 0;
	width: 100%;
	background: #fff;
	z-index: 2;
}
.system-box .right-side{
	width: 100%;
}
input[type="radio"], input[type="checkbox"]{
	position: relative;
	top: 2px;
}
input[type="text"], select{
	width: 160px;
}
.tdw-200{
	width: 200px;
}
.tdw-300{
	width: 300px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" Runat="Server">
	<div class="system-box">
		<div class="right-side">
			<div class="btns-container">
				<div class="modal inmodal" id="addWindow" tabindex="-1" role="dialog" aria-hidden="true">
		        	<form id="checkModuleForm" method="get" action="#">
						<div class="modal-dialog">
							<div class="modal-content animated bounceInRight">
		                    	<div class="modal-header">
									<span class="editTitle">增加修改设置项</span>
									<button type="button" class="close" data-dismiss="modal">
										<span aria-hidden="true">&times;</span>
										<span class="sr-only">关闭</span>
		                    		</button>
								</div>
		                    	<div class="modal-body">
									<table class="module-info" cellpadding="0" cellspacing="0">
										<thead></thead>
										<tbody>
			                                <tr>
												<td colspan="2">
													<div class="form-group">
														<label class="control-label">　设置项<span class="must-sign">*</span></label>
														<select id="DictName" name="DictName" class="form-control input-w240"/>
													</div>
												</td>
											</tr>
			                                <tr>
												<td colspan="2">
													<div class="form-group">
														<label class="control-label">控件类型<span class="must-sign">*</span></label>
														<span><input type="radio" class="controlType" name="ControlType" value="1"/>&nbsp;文本框&nbsp;&nbsp;</span>
			                                            <input type="radio" class="controlType" name="ControlType" value="2"/>&nbsp;下拉框&nbsp;&nbsp;
			                                            <input type="radio" class="controlType" name="ControlType" value="3"/>&nbsp;单选&nbsp;&nbsp;
			                                            <input type="radio" class="controlType" name="ControlType" value="4"/>&nbsp;多选&nbsp;&nbsp;
													</div>
												</td>
											</tr>
			                                <tr>
												<td colspan="2">
													<div class="form-group">
														<label class="control-label">是否启用<span class="must-sign">&nbsp;&nbsp;</span></label>
														<input type="checkbox" name="IsRemove" id="IsRemove" checked="checked"/>
														<div class="label-text label-inline">
	                                                        <i class="fa fa-lightbulb-o "></i> 注意：启用后，各页面将按照此规则执行。
	                                                    </div>
													</div>
												</td>
											</tr>
		                                </tbody>
		                            </table>
		                        </div>
			                    <div class="modal-footer">
									<button type="button" id="addModuleBtn" class="btn btn-primary">保存</button>
									<button type="button" id="closeAddBox" class="btn btn-white" data-dismiss="modal">关闭</button>
								</div>
		                	</div>
						</div>
					</form>
		    	</div>
			</div>
			
			<div class="right-content-box">
				<div class="ibox-content">
					<table class="footable table table-stripped table-list">
						<thead></thead>
						<tbody id="pageList"></tbody>
					</table>
				</div>
			</div>
			
		</div>
	</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" Runat="Server">
    <script src="../assets/js/page/pageControl.js"></script>
    <script src="../assets/js/page/tableControl.js"></script>
    <script src="../assets/js/page/rScroll.js"></script>
    <script type="application/javascript">
        $(document).ready(function () {
            //模块ID
            var ModuleID = Y.getUrlParam("ModuleID");
            //加载按钮
            commonUtil.loadBtnList(ModuleID, function () {
                $('#editBtn').attr("disabled", "disabled");
                rScroll();
            });

            var _isRemove = null;
            var _length = -1;
            var _result = null;
            var _dictValue = "";
            var _dictName = "";
            var _controlType = -1;
            var _isCreat = null;
            var _text = null;
            //获取列表数据
            var loadConfigList = function (deptId, needLayer) {
                if ($('.footable thead').children().size() == 0){
                	tableControl.initThead('.footable',[
						{trClass:'', children:[
							{tdText:'设置项', c:0 , r:0, tdHtml:'', tdId:'', tdClass:'tdw-200'},
							{tdText:'值', c:0 , r:0, tdHtml:'', tdId:'', tdClass:'tdw-300'},
							{tdText:'是否启用', c:0 , r:0, tdHtml:'', tdId:'', tdClass:'tdw-200'},
							{tdText:'&nbsp;', c:0 , r:0, tdHtml:'', tdId:'', tdClass:''},
						]}
					])
                }
                    
                Y.API2('Set', 'GetConfigList', {
                }, function (result) {
                    if (result.Success == false) {
                        swal(result.Message, '', 'error');
                    } else {
                        var tBody = [];
                        if (!Y.checkNull(result)) {
                            if (result.length > 0) {
                                _result = result;
                                result.map(function (item, i) {
                                    $("#Select" + i).empty();
                                    var html = "";
                                    switch (item.ControlType) {
                                        case 1:
                                            html += '<input class="textName" type="text" value="' + (!Y.checkNull(item.Childrens) ? item.Childrens[0].DictName : '') + '"/>';
                                            html += '<input name="childValue" type="hidden" value="' + (!Y.checkNull(item.Childrens) ? item.Childrens[0].DictValue : '') + '"/>';
                                            html += '<input name="childID" type="hidden" value="' + (!Y.checkNull(item.Childrens) ? item.Childrens[0].ID : '') + '"/>';
                                            break;
                                        case 2:
                                            html += '<select class="selectName" id="Select' + i + '" >';
                                            html += "<option value='-1'>请选择</option>";
                                            item.Childrens.map(function (it) {
                                                html += '<option value="' + it.DictValue + '">' + it.DictName + '</option>'
                                            })
                                            html += '</select>';
                                            break;
                                        case 3:
                                            item.Childrens.map(function (it) {
                                                html += '<input class="radioName" type="radio" value="' + it.DictValue + '" name = "radio' + i + '"/>' + it.DictName + '&nbsp;&nbsp;';
                                            });
                                            break;
                                        case 4:
                                            item.Childrens.map(function (it) {
                                                html += '<input class="checkboxName" type="checkbox" value="' + it.DictValue + '" name = "checkbox' + i + '"/>' + it.DictName + '&nbsp;&nbsp;';
                                            });
                                            break;
                                        default: break;
                                    }
                                    var tempTr = {
                                        trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd'),
                                        children: [
                                            { tdText: item.DictValue, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                            { tdText: (item.DictName || ' '), c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                            { tdText: item.ControlType, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                            { tdText: '', c: 0, r: 0, tdHtml: html, tdId: '', tdClass: '' },
                                            { tdText: (item.ConfigIsRemove ? " 禁用" : " 启用"), c: 0, r: 0, tdHtml: '<i class="fa fa-'+ (item.ConfigIsRemove ? "times text-danger" : "check text-navy") +'"></i>', tdId: '', tdClass: '' },
                                            { tdText: item.Childrens.length, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                            { tdText: item.ID, c: 0, r: 0, tdHtml: '', tdId: '', tdClass: 'hide' },
                                            { tdText: '&nbsp;', c: 0, r: 0, tdHtml: '', tdId: '', tdClass: '' },
                                        ]
                                    };
                                    tBody.push(tempTr);
                                })
                            }
                            rScroll();
                        }
                        tableControl.initTbody('.footable', (tBody.length > 0 ? tBody : null), 3, null);
                    }
                    assignment();
                });
            }
            //给列表数据赋值
            var assignment = function () {
                if (!Y.checkNull(_result)) {
                    _result.map(function (item, i) {
                        switch (item.ControlType) {
                            case 2:
                                if (!Y.checkNull(item.Value)) {
                                    $("#Select" + i + " option[value='-1']").remove();
                                    $("#Select" + i).val(item.Value);
                                }
                                break;
                            case 3: $(".radioName[name=radio" + i + "][value=" + item.Value + "]").attr("checked", "checked");
                                break;
                            case 4:
                                if (!Y.checkNull(item.Value)) {
                                    var arr = item.Value.split(',');
                                    arr.map(function (item, index) {
                                        $(".checkboxName[name=checkbox" + i + "]").each(function () {
                                            if ($(this).val() == item) {
                                                $(this).attr("checked", "checked");
                                            }
                                        });
                                    });
                                }
                                break;
                            default: break;
                        }
                    });
                }
            }
            //选中记录
            $('#pageList').on('click', 'tr', function () {
                _dictValue = $(this).find('td:first').text();
                _dictName = $(this).find('td:eq(1)').text();
                _controlType = $(this).find('td:eq(2)').text();
                _isRemove = $(this).find('td:eq(4)').text() == "禁用" ? true : false;
                _length = $(this).find('td:eq(5)').text();
                if (!Y.checkNull(_dictValue))
                    $('#editBtn').removeAttr('disabled');
                $('.footable tr').removeClass('check');
                $(this).addClass('check');
            });
            //获取字典数据
            var loadDicList = function ()
            {
                layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
                Y.API2('Set', 'GetDicList', { }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        $("#DictName").empty();
                        var html = "<option value=-1>请选择</option>";
                        data.map(function(it){
                            html += '<option value="' + it.DictValue + '">' + it.DictName + '</option>'
                        });
                        $("#DictName").append(html);
                    }
                });
            }
            //添加按钮操作
            $('.btns-container').on('click', '#addBtn', function () {
                _isCreat = true;
                $(".controlType[value=1]").click();
                $("#DictName").removeAttr('disabled');
                loadDicList();
            });
            //修改按钮操作
            $('.btns-container').on('click', '#editBtn', function () {
                _isCreat = false;
                $("#DictName").empty();
                $("#DictName").attr('disabled', 'disabled');
                var html = '<option value="' + _dictValue + '">' + _dictName + '</option>'
                $("#DictName").append(html);
                if (_isRemove) {
                    $("#IsRemove").removeAttr("checked");
                }
                else {
                    $("#IsRemove").attr("checked", "checked");
                }
                $(".controlType[value=1]").parent().show();
                switch (_controlType)
                {
                    case "1": $(".controlType[value=1]").click();
                        break;
                    case "2": $(".controlType[value=2]").click();
                        if (_length > 1) {
                            $(".controlType[value=1]").parent().hide();
                        }
                        break;
                    case "3": $(".controlType[value=3]").click();
                        if (_length > 1) {
                            $(".controlType[value=1]").parent().hide();
                        }
                        break;
                    case "4": $(".controlType[value=4]").click();
                        if (_length > 1) {
                            $(".controlType[value=1]").parent().hide();
                        }
                        break;
                    default: break;
                }
            });
            //保存操作
            $("#addModuleBtn").click(function () {
                var deal = "";
                var dictValue = "";
                var successText = "";
                var ControlType = $('.controlType:checked').val();
                var IsRemove = $("#IsRemove").is(':checked') ? false : true;
                if (_isCreat) {
                    dictValue = $("#DictName option:selected").val();
                    if (dictValue == -1) {
                        swal('请选择一个设置项！');
                        return false;
                    }
                    deal = "CreatConfig";
                    successText = '新增成功';
                }
                else {
                    deal = "ModifyConfig";
                    dictValue = _dictValue;
                    successText = '编辑成功';
                }
                layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
                Y.API2('Set', deal, {
                    dictValue: dictValue,
                    ControlType: ControlType,
                    IsRemove: IsRemove,
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal(successText, '', 'success');
                        //关闭新建  or 编辑框
                        $('#closeAddBox').click();
                        //刷新列表
                        loadConfigList();
                    }
                })
            });

            loadConfigList();

            $('#pageList').on('change', '.selectName', function () {
                var Value = $(this).val();
                if (Value == "-1")
                {
                    swal('请选择一个值！');
                    return false;
                }
                var DictValue = $(this).parents('tr').find('td:first').text();
                layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
                Y.API2("Set", "ModifyByDictValue", {
                    dictValue: DictValue,
                    Value: Value,
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal("修改成功！", '重新登录后生效', 'success');
                        //刷新列表
                        loadConfigList();
                    }
                });
            });



            $('#pageList').on('focus', '.textName', function () {
                _text = $(this).val();
            });

            $('#pageList').on('blur', '.textName', function () {
                var deal = "";
                var param =null;
                var Value = $(this).val();
                if (Y.checkNull(Value))
                {
                    swal('请输入一个值！');
                    return false;
                }
                var DictValue = $(this).parents('tr').find('td:first').text();
                var ParentId = $(this).parents('tr').find('td:eq(6)').text();
                var ID = $(this).parents().find("input[type=hidden][name=childID]").val();
                if (Y.checkNull(_text)) {
                    deal = "Create";
                    param= {
                        DictName: Value,
                        DictValue: Value,
                        ParentId: ParentId,
                    };
                }
                else {
                    deal = "Modify";
                    param = {
                        DictName: Value,
                        DictValue: Value,
                        ParentId: ParentId,
                        ID: ID,
                    };
                }
                if(_text == Value)
                {
                    return false;
                }
                layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
                Y.API2("SysDict", deal, param, function (data) {
                    if (data.Success == false) {
                        layer.closeAll('dialog');
                        swal(data.Message, '', 'error');
                    } else {
                        Y.API2("Set", "ModifyByDictValue", {
                            dictValue: DictValue,
                            Value: Value,
                        }, function (result) {
                            layer.closeAll('dialog');
                            if (result.Success == false) {
                                swal(result.Message, '', 'error');
                            } else {
                                swal("修改成功！", '重新登录后生效', 'success');
                                //刷新列表
                                loadConfigList();
                            }
                        });
                    }
                });
            });

            $('#pageList').on('click','.radioName',function () {
                var Value = $(this).val();
                var DictValue = $(this).parents('tr').find('td:first').text();
                layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
                Y.API2("Set", "ModifyByDictValue", {
                    dictValue: DictValue,
                    Value: Value,
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal("修改成功！", '重新登录后生效', 'success');
                        //刷新列表
                        loadConfigList();
                    }
                });
            });

            $('#pageList').on('click', '.checkboxName', function () {
                var Value = "";
                $(".checkboxName:checked").each(function () {
                    if ($(this).is(":checked"))
                    {
                        Value += $(this).val() + ',';
                    }
                });
                var DictValue = $(this).parents('tr').find('td:first').text();
                layer.msg('玩命加载中……', {icon: 16, shade: 0.5, time:0, maxWidth:200});
                Y.API2("Set", "ModifyByDictValue", {
                    dictValue: DictValue,
                    Value: Value,
                }, function (data) {
                    layer.closeAll('dialog');
                    if (data.Success == false) {
                        swal(data.Message, '', 'error');
                    } else {
                        swal("修改成功！", '重新登录后生效', 'success');
                        //刷新列表
                        loadConfigList();
                    }
                });
            });
        });
    </script>
</asp:Content>